<template>
  <div class="">
    <el-table :data="filterTableData" style="width: 100%">
    <el-table-column label="注册日期" prop="createTime" />
    <el-table-column label="名字" prop="userName" />
    <el-table-column label="地址" prop="address" />
    <el-table-column label="余额" prop="account" />
    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="搜索" />
      </template>
      <template #default="scope">
        <el-button  @click="handleEdit(scope.$index, scope.row)" type="danger"
          >充值</el-button>
      </template>
    </el-table-column>
  </el-table>
  </div>
  <div >    
    <div class="footer">
    <el-pagination  @current-change="currentChange" 
    :total="totalValue" 
    :default-current-page="1" 
    v-model:current-page="currentPage" 
    :page-size="pageSize" 

    layout="prev, pager, next"  />
    </div>
    </div>

    <el-dialog
    v-model="chargeDialog"
    title="Tips"
    width="30%"
  >
  <el-input-number v-model="chargeUser.money" :precision="2" :step="100" :max="999999" />
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="doCharge">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">

import { ElMessage } from 'element-plus';
import { computed, ref } from 'vue'
import { count, listPage ,charge } from '../../api/user'


const totalValue = ref()
const pageSize = ref(10)
const currentPage =  ref(1)
const chargeDialog = ref(false)
const chargeUser = ref({
  userName:'',
  money:0
})

const search = ref('')


const filterTableData = computed(() =>
  tableData.value.filter(
    (data:any) =>
      !search.value ||
      data.userName.toLowerCase().includes(search.value.toLowerCase())
  )
)


const currentChange = (page:number) => {
    listPage((page - 1) * pageSize.value,pageSize.value).then(res => {
    tableData.value = res.data.data
    console.log(tableData.value);
    
})
}
count().then(res => {
    totalValue.value = res.data.data
    console.log(totalValue.value + '------------');
    
})

listPage(0,pageSize.value).then(res => {
    tableData.value = res.data.data
    console.log(tableData.value);
    
})

const handleEdit = (index: number, row: any) => {
  console.log(index, row)
  chargeUser.value.userName = row.userName
  chargeDialog.value = true;
}

const doCharge = () => {
  charge(chargeUser.value).then(res => {
    if(res.data.code == 200) {
      ElMessage({
        message:res.data.msg,
        type:'success'
      })
      chargeDialog.value = false
      listPage((currentPage.value - 1) * pageSize.value,pageSize.value).then(res => {
    tableData.value = res.data.data
    console.log(tableData.value);
    
})
    }else {
      ElMessage({
        message:res.data.msg,
        type:'error'
      })
    }
  })
}
const handleDelete = (index: number, row: any) => {
  console.log(index, row)
}

const tableData = ref<Array<any>>([])
</script>

<style scoped >
.footer {
    width: 700px;
    margin: 0 auto;
}
</style>